package com.jed.service;

import com.jed.dao.UserDao;
import com.jed.util.PasswordUtil;
import com.jed.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userApi")
public class UserService {

	@Autowired
	private UserDao dao;

	@RequestMapping("/login")
	public Map<String, Object> login(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if (StringUtil.isNotEmpty(username) && StringUtil.isNotEmpty(password)) {
			password = PasswordUtil.encrypt(username, password, PasswordUtil.getStaticSalt());
			List<Map<String, Object>> userList = dao.getUserByName(username, password);
			if (StringUtil.isNotEmpty(userList) && userList.size() > 0) {
				map.put("user", userList.get(0));
				map.put("result", "用户名验证通过");
				map.put("resultType", "0");
			} else {
				map.put("result", "用户名或密码不正确");
				map.put("resultType", "101");
			}
		}else {
			map.put("result", "登录失败");
			map.put("resultType", "102");
		}
		return map;
	}

}
